package com.aqie.arithmetic.array;

import edu.princeton.cs.algs4.BinarySearch;
import edu.princeton.cs.algs4.StdOut;

import java.util.Arrays;

/**
 * 线性对数 级别 NlogN
 */
public class TwoSumFast {
    public static int count(int[] arr){
        Arrays.sort(arr);
        int N = arr.length;
        int cnt = 0;
        for (int i = 0; i < N; i++){
            if (BinarySearch.indexOf(arr, -arr[i]) > i) cnt++;
        }
        return cnt;
    }

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,-2,-3};
        StdOut.println(count(arr));
    }
}
